<template>
  <div class="wrapper">
    <div style="margin:90px auto;background-color: aliceblue; width: 350px; height: 480px;padding:20px;border-radius: 5px">
      <div style="margin: 10px 0;padding-bottom:20px;color: #606266; text-align: center;font-size: 30px"><b>注 册</b></div>
      <el-form status-icon :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username" >
          <el-input size="medium"  placeholder="请输入用户名" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input size="medium" placeholder="请输入电话" prefix-icon="el-icon-phone" v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input size="medium" placeholder="请输入邮箱" prefix-icon="el-icon-position" v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" placeholder="密码" prefix-icon="el-icon-lock" show-password
                    v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input size="medium" placeholder="确认密码" prefix-icon="el-icon-lock"
                    show-password
                    v-model="user.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="primary" size="medium" style="width: 100%" autocomplete="off" @click="login">确定注册
          </el-button>

        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="warning" size="medium" style="width: 100%" autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>

export default {
  name: "Login",
  data() {
    // 表单验证
    let checkPhone=(rule,value,callback)=>{
      if(!value){
        return callback(new Error('请输入电话'));
      }else if(value.length!==11){
        return callback(new Error('电话号码格式错误'))
      }
      let f=/^[1][34578][0-9]{9}$/  //电话号码格式
      if(f.test(value) === false){
        callback(new Error('电话号码格式错误'))
      }
      callback()
    }
    let validatePassword=(rule,value,callback)=>{
      if(value===''){
        callback(new Error('请输入密码'));
      }else{
        if(this.user.confirmPassword !==''){
          this.$refs.userForm.validateField('confirmPassword');
        }
        callback();
      }
    }
    let validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      user: {
        username: '',
        phone:'',
        email:'',
        password: '',
        confirmPassword:''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: ['blur','change']}
        ],
        phone:[
          {required: true, message: '请输入电话号码', trigger: 'blur'},
          {validator:checkPhone, trigger: ['blur','change']}
        ],
        email:[
          {required: true, message: '请输入电话号码', trigger: 'blur'},
          {type:'email', message: '请输入正确的邮箱地址',trigger: ['blur','change']}
        ],
        password: [
          {validator:validatePassword, trigger: 'blur'}
        ],
        confirmPassword: [
          {validator:validateConfirmPassword, trigger: ['blur','change']}
        ],
      }
    }
  },
  methods: {
    login() {

      this.$refs['userForm'].validate((valid) => {
        if (valid) { //表单校验合法
          this.request.post("/user/register", this.user).then(res => {
            if (res.code==='200') {
              // localStorage.setItem("user",JSON.stringify(res.data))//存储用户信息到浏览器
              // this.$router.push("/")
              this.$message.success("注册成功，请登录")
              this.$router.push("/")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });


    }
  }
}
</script>

<style scoped>
.wrapper {
  height: 100vh;
  //background-image: linear-gradient(to bottom right, #FC4668, #3F5EF8);
  overflow: hidden;
  background-color:rgba(200,224,228,0.8);
}
</style>








